package com.hzhk.happyrent.ui.view.listview;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzhk.happyrent.R;


/**
 * maimai_a
 * cn.maitian.app.ui.view.listview
 *
 * @Author: xie
 * @Time: 2016/7/6 12:45
 * @Description:
 */

public class MaiTianListViewHeader extends LinearLayout {

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_REFRESHING = 2;
    public final static int STATE_COMPLETE = 3;
    public String headerDown = getResources().getString(R.string.headerDown);
    public String headerReady = getResources().getString(R.string.headerReady);
    public String headerUp = getResources().getString(R.string.headerUp);
    private ImageView mFirstView;
    private ImageView mSecondView;
    private AnimationDrawable secondAnim;
    private TextView refreshTv;
    private LinearLayout mContainer;
//    private int[] imgs = {R.mipmap.seeding_small, R.mipmap.seeding1_small, R.mipmap.seeding2_small, R.mipmap.seeding3_small, R.mipmap.seeding4_small
//            , R.mipmap.seeding5_small, R.mipmap.seeding6_small, R.mipmap.seeding7_small, R.mipmap.seeding8_small, R.mipmap.seeding9_small
//            , R.mipmap.seeding10_small, R.mipmap.seeding11_small, R.mipmap.seeding12_small, R.mipmap.seeding13_small, R.mipmap.seeding14_small
//            , R.mipmap.seeding15_small, R.mipmap.seeding16_small, R.mipmap.seeding17_small, R.mipmap.seeding18_small, R.mipmap.seeding19_small
//            , R.mipmap.seeding20_small, R.mipmap.seeding21_small, R.mipmap.seeding22_small, R.mipmap.seeding23_small, R.mipmap.seeding24_small};

    private int[] imgs = {R.mipmap.loading2, R.mipmap.loading3, R.mipmap.loading4 ,R.mipmap.loading5};
    public MaiTianListViewHeader(Context context) {
        super(context);
        initView(context);
    }

    public MaiTianListViewHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MaiTianListViewHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {

        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
        mContainer = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.layout_maitian_listview_header, null);
        addView(mContainer, lp);
        setGravity(Gravity.CENTER);
        mFirstView = (ImageView) mContainer.findViewById(R.id.first_view);
        refreshTv = (TextView) mContainer.findViewById(R.id.tv_pull_to_refresh);
        mSecondView = (ImageView) mContainer.findViewById(R.id.second_view);
        mSecondView.setImageResource(R.drawable.pull_refresh_third_anim);
        secondAnim = (AnimationDrawable) mSecondView.getDrawable();

    }

    public void setAnimation(float currentProgress) {
        float a = currentProgress * 100;
        int b = (int) (a / 25);
        if (b < imgs.length)
            mFirstView.setImageResource(imgs[b]);
    }

    public void setState(int state) {
        switch (state) {
            case STATE_NORMAL:
                //设置文字为下拉刷新
                refreshTv.setText(headerDown);
                //第一状态view显示出来
                mFirstView.setVisibility(VISIBLE);
                //第二状态view隐藏起来
                mSecondView.setVisibility(GONE);
                //第二状态动画停止
                secondAnim.stop();
                break;
            case STATE_READY:
                //文字显示为放开刷新
                refreshTv.setText(headerReady);
                //第一状态view隐藏起来
//                mFirstView.setVisibility(VISIBLE);
                //第二状态view显示出来
//                mSecondView.setVisibility(VISIBLE);
                break;
            case STATE_REFRESHING:
                //文字设置为正在刷新
                refreshTv.setText(headerUp);
                //第一状态view隐藏起来
                mFirstView.setVisibility(GONE);
                //第二状态view显示起来
                mSecondView.setVisibility(VISIBLE);
                //启动第二状态动画
                secondAnim.start();
                break;
            case STATE_COMPLETE:
                //刷新完毕
                //停止第二个动画
                secondAnim.stop();
                break;
            default:
                break;
        }

    }

    public int getVisiableHeight() {
        return mContainer.getHeight();
    }

    public void setVisiableHeight(int height) {
        if (height < 0) height = 0;
        LayoutParams lp = (LayoutParams) mContainer.getLayoutParams();
        lp.height = height;
        mContainer.setLayoutParams(lp);
    }
}
